var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/hellom";
 //修改
function xiugai(jihe,shuju,gaiju){
    return new Promise((res,rej) =>{
        MongoClient.connect(url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("hellom");
            dbo.collection(jihe).updateOne(shuju, {$set: gaiju}, function(err, result) {
              if (err) throw err;
              // console.log("文档更新成功");
              res(result);
              db.close();
          });
        });
    })  
}
//查找
function find(jihe,shuju){
    return  new Promise((res,rej)=>{
      MongoClient.connect(url, function(err, db) {
        if (err) throw err;
        // console.log("数据库已创建!");
        var dbo = db.db('hellom');
        dbo.collection(jihe).find(shuju).toArray(function(err, result) { // 返回集合中所有数据
            if (err) throw err;
            res(result);
            // console.log(result);
            db.close();
        });
      });
    })
  }


//插入
function cha(jihe,myobj){
    return new Promise((res,rej)=>{
        MongoClient.connect(url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("hellom");
            // var myobj = { name: "菜鸟教程", url: "www.runoob" };
            dbo.collection(jihe).insertOne(myobj, function(err, result) {
                if (err) throw err;
                console.log("文档插入成功");
                res(result)
                db.close();
            });
        });
    })
}

 module.exports={find,cha,xiugai}